package com.hankol.message.imConfig

import android.content.Context
import android.graphics.Color
import android.text.Spannable
import android.text.SpannableString
import android.text.TextUtils
import android.util.TypedValue
import android.view.LayoutInflater
import android.view.View
import android.view.View.OnLongClickListener
import android.view.ViewGroup
import android.widget.TextView
import androidx.core.text.TextUtilsCompat
import com.baselibrary.bean.IMUserBean
import com.baselibrary.extension.checkNotNull
import com.baselibrary.utils.GsonUtils
import com.baselibrary.utils.SpUtils
import com.baselibrary.utils.TimeUtil
import com.hankol.message.R
import io.rong.imkit.conversation.extension.component.emoticon.AndroidEmoji
import io.rong.imkit.conversation.messgelist.provider.BaseMessageItemProvider
import io.rong.imkit.model.UiMessage
import io.rong.imkit.userinfo.RongUserInfoManager
import io.rong.imkit.widget.adapter.IViewProviderListener
import io.rong.imkit.widget.adapter.ViewHolder
import io.rong.imlib.model.Message
import io.rong.imlib.model.MessageContent
import io.rong.message.TextMessage
import java.util.*

//为了给头像加上vip标签，需要替换掉内置的模版
class CustomTextMessageProvider : BaseMessageItemProvider<TextMessage>() {
    init {
        this.mConfig.showReadState = true
    }

    override fun onCreateMessageContentViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val textView = LayoutInflater.from(parent.context).inflate(R.layout.rc_text_message_item, parent, false)

        return ViewHolder(parent.context, textView)
    }

    override fun bindMessageContentViewHolder(holder: ViewHolder, parentHolder: ViewHolder, message: TextMessage, uiMessage: UiMessage, position: Int, list: List<UiMessage>, listener: IViewProviderListener<UiMessage>) {
        val view = holder.getView(R.id.rc_text) as TextView
        if (TextUtilsCompat.getLayoutDirectionFromLocale(Locale.getDefault()) === 1) {
            view.textAlignment = View.TEXT_ALIGNMENT_VIEW_END
        }

        view.tag = uiMessage.messageId
        //        if (uiMessage.contentSpannable == null) {
        //            val spannable = TextViewUtils.getSpannable(message.getContent(), RegularCallBack { spannable ->
        //                uiMessage.contentSpannable = spannable
        //                if (view.tag == uiMessage.messageId) {
        //                    view.post { view.text = uiMessage.contentSpannable }
        //                }
        //            })
        //            uiMessage.contentSpannable = spannable
        //        }
        //        view.text = uiMessage.contentSpannable
        view.text = message.getContent()
        //        view.movementMethod = LinkTextViewMovementMethod(ILinkClickListener { link ->
        //            var result = false
        //            if (RongConfigCenter.conversationConfig().conversationClickListener != null) {
        //                result = RongConfigCenter.conversationConfig().conversationClickListener.onMessageLinkClick(holder.getContext(), link, uiMessage.message)
        //            }
        //
        //            if (result) {
        //                true
        //            } else {
        //                val str = link.toLowerCase()
        //                if (str.startsWith("http") || str.startsWith("https")) {
        //                    RouteUtils.routeToWebActivity(view.context, link)
        //                    result = true
        //                }
        //
        //                result
        //            }
        //        })
        view.setOnClickListener(object : View.OnClickListener {
            override fun onClick(view: View) {
                val parent = view.getParent()
                if (parent is View) {
                    (parent as View).performClick()
                }

            }
        })
        view.setOnLongClickListener(object : OnLongClickListener {
            override fun onLongClick(view: View): Boolean {
                val parent = view.getParent()
                return if (parent is View) (parent as View).performLongClick() else false
            }
        })
        view.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 16f)
        if (uiMessage.getMessageDirection() == Message.MessageDirection.SEND) {
            view.setTextColor(Color.WHITE);
        } else {
            view.setTextColor(Color.BLACK);
        }
    }

    override fun onItemClick(holder: ViewHolder, message: TextMessage, uiMessage: UiMessage, position: Int, list: List<UiMessage>, listener: IViewProviderListener<UiMessage>): Boolean {
        return false
    }

    override fun isMessageViewType(messageContent: MessageContent): Boolean {
        return messageContent is TextMessage && !messageContent.isDestruct
    }

    override fun getSummarySpannable(context: Context, message: TextMessage): Spannable {
        if (message != null && !TextUtils.isEmpty(message.getContent())) {
            var content = message.getContent()
            content = content.replace("\n", " ")
            if (content.length > 100) {
                content = content.substring(0, 100)
            }

            return SpannableString(AndroidEmoji.ensure(content))
        } else {
            return SpannableString("")
        }
    }

    override fun bindViewHolder(holder: ViewHolder, uiMessage: UiMessage, position: Int, list: MutableList<UiMessage>, listener: IViewProviderListener<UiMessage>) {
        super.bindViewHolder(holder, uiMessage, position, list, listener)
        holder.setText(R.id.rc_time,TimeUtil.fromToday(uiMessage.sentTime))
        if (uiMessage.senderUserId.checkNotNull() && RongUserInfoManager.getInstance().getUserInfo(uiMessage.senderUserId).checkNotNull() && RongUserInfoManager.getInstance().getUserInfo(uiMessage.senderUserId).extra.checkNotNull()) {
            val isFans = GsonUtils.parseJsonToBean(RongUserInfoManager.getInstance().getUserInfo(uiMessage.senderUserId).extra, IMUserBean::class.java).isFans == 1
            holder.setVisible(R.id.img_vip_tag_right, ("${SpUtils.getInstance().loadUserBean.data.id}").equals(uiMessage.senderUserId) && isFans && (uiMessage.messageDirection == Message.MessageDirection.SEND))
            holder.setVisible(R.id.img_vip_tag_left, (!("${SpUtils.getInstance().loadUserBean.data.id}").equals(uiMessage.senderUserId)) && isFans)
        }
    }
}
